import Vue from 'vue'
// import store from '../store/index.js'
import VueRouter from 'vue-router'
import store from '../store/index'

Vue.use(VueRouter)
const Login = () => import('components/Login.vue')
const register = () => import('components/register.vue')
const welcome = () => import('components/welcome.vue')
const Home = () => import('components/Home.vue')
const user = () => import('components/User/user.vue')
const student = () => import('components/Student/student.vue')
const studentDormList = () => import('components/Student/studentDormList.vue')
const DormDamageUp = () => import('components/dorm/DormDamageUp.vue')
const dormDamageCost = () => import('components/dorm/dormDamageCost.vue')
const dormAllDamageCost = () => import('components/dorm/dormAllDamageCost.vue')
const DormVisit = () => import('components/dorm/DormVisit.vue')
const DormVisitList = () => import('components/dorm/DormVisitList.vue')
const DormVisitAll = () => import('components/dorm/DormVisitAll.vue')
const DormInfo = () => import('components/dorm/DormInfo.vue')
const currDormInfo = () => import('components/dorm/currDormInfo.vue')
const Lanyuan = () => import('components/tower/lanyuan/Lanyuan.vue')
const Taoyuan = () => import('components/tower/taoyuan/Taoyuan.vue')
const Liyuan = () => import('components/tower/liyuan/Liyuan.vue')
const LogisticsRepair = () => import('components/Logistics/LogisticsRepair.vue')
const DataStaView = () => import('components/dataSta/DataStaView.vue')
const dataStaGeneral = () => import('components/dataSta/dataStaGeneral.vue')
const StudentDorm = () => import('components/studentDorm/StudentDorm.vue')
const dormitoryHygiene = () => import('components/studentDorm/dormitoryHygiene.vue')
const damageUp = () => import('components/damageUp/damageUp.vue')
const CBlate = () => import('components/dorm/CBlate.vue')
const CBLall = () => import('components/dorm/CBLall.vue')
const Inform = () => import('components/dorm/Inform.vue')
const CBLstudents = () => import('components/dorm/CBLstudents.vue')
const damageInfo = () => import('components/damageUp/damageInfo.vue')
const rightControl = () => import('components/Logistics/rightControl.vue')

const userManger = () => import('components/User/userManger.vue')
const studentManger = () => import('components/Student/studentManger.vue')
const towerManger = () => import('components/tower/towerManger.vue')
const studentDormManger = () => import('components/studentDorm/studentDormManger.vue')
const logisticsManger = () => import('components/Logistics/logisticsManger.vue')
const dormManger = () => import('components/dorm/dormManger.vue')
const dataStaManger = () => import('components/dataSta/dataStaManger.vue')
const damageUpManger = () => import('components/damageUp/damageUpManger.vue')

// 默认路由
export const defaultRouter = [
  {
    // 路由重定向
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login',
    component: Login
  },
  {
    path: '/register',
    component: register
  },
  {
    path: '/home',
    component: Home,
    // welcome界面为home的子路由,并重定向到welcome界面
    redirect: '/welcome',
    children: [
      {
        path: '/welcome',
        component: welcome
      },
      {
        path: '/profileManger',
        component: userManger,
        meta: {
          id: '100',
          name: '用户管理',
          path: 'profile',
          icon: 'el-icon-user'
        },
        children: [
          {
            path: '/profile',
            component: user,
            meta: {
              id: '101',
              name: '我的',
              path: 'profile'
            }
          }
        ]
      },
      {
        path: '/studentDormManger',
        component: studentDormManger,
        meta: {
          id: '160',
          name: '学生宿舍',
          icon: 'el-icon-user'
        },
        children: [
          {
            path: '/myStudentDorm',
            component: StudentDorm,
            meta: {
              id: '161',
              name: '我的宿舍',
              path: 'myStudentDorm'
            }
          },
          {
            path: '/dormitoryHygiene',
            component: dormitoryHygiene,
            meta: {
              id: '162',
              name: '宿舍评分信息',
              path: 'dormitoryHygiene'
            }
          }
        ]
      },
      {
        path: '/damageUpManger',
        component: damageUpManger,
        meta: {
          id: '170',
          name: '损坏上报',
          icon: 'el-icon-user'
        },
        children: [
          {
            path: '/damageUp',
            component: damageUp,
            meta: {
              id: '171',
              name: '损坏上报',
              path: 'damageUp'
            }
          },
          {
            path: '/damageInfo',
            component: damageInfo,
            meta: {
              id: '172',
              name: '维修信息',
              path: 'damageInfo'
            }
          }
        ]
      }
    ]
  }
]
// 超级管理员
export const adminRouters = [
  {
    path: '/towerManger',
    component: towerManger,
    meta: {
      id: '110',
      name: '楼宇管理',
      icon: 'el-icon-location'
    },
    children: [
      {
        path: '/taoyuan',
        component: Taoyuan,
        meta: {
          id: '111',
          name: '桃园',
          path: 'taoyuan'
        }
      }, {
        path: '/lanyuan',
        component: Lanyuan,
        meta: {
          id: '112',
          name: '兰园',
          path: 'lanyuan'
        }
      }, {
        path: '/liyuan',
        component: Liyuan,
        meta: {
          id: '113',
          name: '荔园',
          path: 'liyuan'
        }
      }
    ]
  },
  {
    path: '/dormManger',
    component: dormManger,
    meta: {
      id: '120',
      name: '宿舍管理',
      icon: 'el-icon-s-home'
    },
    children: [
      {
        path: '/Inform',
        component: Inform,
        meta: {
          id: '127',
          name: '通知发布和查看',
          path: 'Inform'
        }
      },
      {
        path: '/dormVisit',
        component: DormVisit,
        meta: {
          id: '121',
          name: '来访人员登记',
          path: 'dormVisit'
        }
      },
      {
        path: '/comeBackLate',
        component: CBlate,
        meta: {
          id: '126',
          name: '晚归登记',
          path: 'comeBackLate'
        }
      },
      {
        path: '/CBLall',
        component: CBLall,
        meta: {
          id: '123',
          name: '晚归总列表',
          path: 'CBLall'
        }
      },
      {
        path: '/DormVisitAll',
        component: DormVisitAll,
        meta: {
          id: '128',
          name: '来访人员总列表',
          path: 'DormVisitAll'
        }
      },
      {
        path: '/logisticsrepair',
        component: LogisticsRepair,
        meta: {
          id: '124',
          name: '维修总列表',
          path: 'logisticsrepair'
        }
      },
      {
        path: '/dormAllDamageCost',
        component: dormAllDamageCost,
        meta: {
          id: '129',
          name: '维修费用总列表',
          path: 'dormAllDamageCost'
        }
      },
      {
        path: '/DormInfo',
        component: DormInfo,
        meta: {
          id: '125',
          name: '宿舍信息',
          path: 'DormInfo'
        }
      }
    ]
  },
  {
    path: '/studentManger',
    component: studentManger,
    meta: {
      id: '130',
      name: '学生管理',
      icon: 'el-icon-s-custom'
    },
    children: [
      {
        path: '/student',
        component: student,
        meta: {
          id: '131',
          name: '学生列表',
          path: 'student'
        }
      }
    ]
  },
  {
    path: '/logisticsManger',
    component: logisticsManger,
    meta: {
      id: '140',
      name: '权限管理',
      icon: 'el-icon-s-platform'
    },
    children: [
      {
        path: '/rightControl',
        component: rightControl,
        meta: {
          id: '142',
          name: '权限控制',
          path: 'rightControl'
        }
      }
    ]
  },
  {
    path: '/dataStaManger',
    component: dataStaManger,
    meta: {
      id: '150',
      name: '数据统计',
      icon: 'el-icon-s-data'
    },
    children: [
      {
        path: '/datastaview',
        component: DataStaView,
        meta: {
          id: '151',
          name: '视图',
          path: 'datastaview'
        }
      }
    ]
  }
]
// 宿舍管理员
export const houseparentRouters = [
  {
    path: '/dormManger',
    component: dormManger,
    meta: {
      id: '120',
      name: '宿舍管理',
      icon: 'el-icon-s-home'
    },
    children: [
      {
        path: '/Inform',
        component: Inform,
        meta: {
          id: '127',
          name: '通知发布和查看',
          path: 'Inform'
        }
      },
      {
        path: '/dormVisit',
        component: DormVisit,
        meta: {
          id: '121',
          name: '来访人员登记',
          path: 'dormVisit'
        }
      },
      {
        path: '/comeBackLate',
        component: CBlate,
        meta: {
          id: '125',
          name: '晚归登记',
          path: 'comeBackLate'
        }
      },
      {
        path: '/DormVisitList',
        component: DormVisitList,
        meta: {
          id: '122',
          name: '来访人员列表',
          path: 'DormVisitList'
        }
      },
      {
        path: '/DormDamageUp',
        component: DormDamageUp,
        meta: {
          id: '123',
          name: '宿舍维修列表',
          path: 'dormDamageUp'
        }
      },
      {
        path: '/dormDamageCost',
        component: dormDamageCost,
        meta: {
          id: '128',
          name: '宿舍维修费用列表',
          path: 'dormDamageCost'
        }
      },
      {
        path: '/CBLstudents',
        component: CBLstudents,
        meta: {
          id: '126',
          name: '晚归列表',
          path: 'CBLstudents'
        }
      },
      {
        path: '/currDormInfo',
        component: currDormInfo,
        meta: {
          id: '124',
          name: '宿舍信息',
          path: 'currDormInfo'
        }
      }
    ]
  },
  {
    path: '/studentManger',
    component: studentManger,
    meta: {
      id: '130',
      name: '学生管理',
      icon: 'el-icon-s-custom'
    },
    children: [
      {
        path: '/studentDormList',
        component: studentDormList,
        meta: {
          id: '132',
          name: '学生列表',
          path: 'studentDormList'
        }
      }
    ]
  },
  {
    path: '/dataStaManger',
    component: dataStaManger,
    meta: {
      id: '150',
      name: '数据统计',
      icon: 'el-icon-s-data'
    },
    children: [
      {
        path: '/dataStaGeneral',
        component: dataStaGeneral,
        meta: {
          id: '151',
          name: '视图',
          path: 'dataStaGeneral'
        }
      }
    ]
  }
]
// 卫生检查员
export const inpectorRouters = [
  {
    path: '/dormManger',
    component: dormManger,
    meta: {
      id: '120',
      name: '宿舍管理',
      icon: 'el-icon-s-home'
    },
    children: [
      {
        path: '/currDormInfo',
        component: currDormInfo,
        meta: {
          id: '124',
          name: '宿舍信息',
          path: 'currDormInfo'
        }
      }
    ]
  }
]
const router = new VueRouter(
  {
    mode: 'history',
    base: process.env.BASE_URL,
    routes: defaultRouter
  })
let flag = 0
// 挂载路由导航卫士
router.beforeEach((to, from, next) => {
  // to 将要访问的路径
  // from 从哪个路径跳转而来
  // next 是一个函数，表示放行
  // next() 放行 next('/login') 强制跳转到/login
  if (to.path === '/login' || to.path === '/register') return next()
  // 获取后端api得来的token,通过是否存在token选择放行
  const tokenStr = window.localStorage.getItem('Authorization')
  if (!tokenStr) {
    return next('/login')
  } else {
    // flag标记，主要用于解决刷新界面空白问题
    if (flag === 0) {
      // console.log('flag', flag)
      // 刷新页面后要确保vuex里面存在角色
      store.dispatch('authority/getRole', window.localStorage.getItem('role'))
      // 将默认路由传给vuex
      store.dispatch('authority/getAddRouters', defaultRouter)
      // 将在vuex拼接好的路由重新赋值给router.options.routes，也就是new VueRouter中的routes
      router.options.routes = store.getters['authority/addrouter']
      // 动态添加路由，必须要这局，不然无法添加上面的拼接好的路由
      router.addRoutes(store.getters['authority/addrouter'])
      flag++
      return next({ ...to, replace: true })
    } else {
      next()
    }
  }
})
export default router
